/**
 * Created by sunzhijun on 2019/01/01.
 */
import React, {Component} from 'react';
import {base, high, promptBox} from 'nc-lightapp-front';
import {hrAjax as ajax} from 'src/hrpub/common/utils/utils';

import './index.less';

const {NCModal, NCButton, NCTable, NCRadio} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;
const moduleId = "hihr6007";
const domainName = "hrhi";

class ReptObjModal extends Component {
    constructor(props) {
        super(props);
        this.closeModal = this.closeModal.bind(this);
        this.state = {
            tableData : [],
            pk_org: '',
            checkedVal: ''  //设置默认值
        }
        this.renderTable = this.renderTable.bind(this);
        this.changeChecked = this.changeChecked.bind(this);
    }

    componentWillMount() {
        let callback = (json, status, inlt) => {
            if (status) {
                this.lang = json
                this.setState({json, inlt})//存json和inlt到页面state中并刷新页面
            }
        }
        this.props.MultiInit.getMultiLang({moduleId: moduleId, domainName: domainName, callback});
    }

    componentDidMount() {
       this.getModalData();
    }

    //保存设置
    saveData(){
        let { checkedVal } = this.state;
        ajax ({
            url: '/nccloud/hrhi/seqcontrol/SetupSeqcontrolAction.do',
            data: {
                'pk_org': this.state.pk_org,
                'pk_seqcontrol_set': checkedVal
            },
            success: res => {
                this.closeModal();
            }
        })
        
    }

    //确认弹窗
    showPromptBox(){
        let { checkedVal } = this.state;
        if(!!checkedVal){
            promptBox({
                color: 'warning',               // 提示类别默认"success"， "success"/"info"/"warning"/"danger",非必输
                title: this.state.json['hihr-000003'],                // 提示标题, 默认不同类别下分别为："已成功"/"帮助信息"/"请注意"/"出错啦",非必输/* 国际化处理： 确认删除*/
                content: this.state.json['hihr-000022'],             // 提示内容,非必输/* 国际化处理： 您确定要删除所选数据么?*/
                noFooter: false,                // 是否显示底部按钮(确定、取消),默认显示(false),非必输
                noCancelBtn: false,             // 是否显示取消按钮,，默认显示(false),非必输
                beSureBtnName: this.state.json['hihr-000006'],          // 确定按钮名称, 默认为"确定",非必输/* 国际化处理： 确定*/
                cancelBtnName: this.state.json['hihr-000007'],         // 取消按钮名称, 默认为"取消",非必输/* 国际化处理： 取消*/
                hasCloseBtn:true,             //显示“X”按钮，默认不显示，不显示是false，显示是true
                beSureBtnClick: ()=> {this.saveData()},   // 确定按钮点击调用函数,非必输
                // cancelBtnClick: functionCancel,  // 取消按钮点击调用函数,非必输
                // closeBtnClick:functionClose, //关闭按钮点击调用函数，非必输
                closeByClickBackDrop:false,//点击遮罩关闭提示框，默认是true点击关闭，阻止关闭是false
            })
        }else{
            promptBox({
                color: 'warning',               // 提示类别默认"success"， "success"/"info"/"warning"/"danger",非必输
                content: this.state.json['hihr-000023'],             // 提示内容,非必输/* 国际化处理： 您确定要删除所选数据么?*/
                noFooter: false,                // 是否显示底部按钮(确定、取消),默认显示(false),非必输
                noCancelBtn: true,             // 是否显示取消按钮,，默认显示(false),非必输
                beSureBtnName: this.state.json['hihr-000024'],         // 取消按钮名称, 默认为"取消",非必输/* 国际化处理： 取消*/
                hasCloseBtn:true,             //显示“X”按钮，默认不显示，不显示是false，显示是true
                // cancelBtnClick: functionCancel,  // 取消按钮点击调用函数,非必输
                // closeBtnClick:functionClose, //关闭按钮点击调用函数，非必输
                closeByClickBackDrop:false,//点击遮罩关闭提示框，默认是true点击关闭，阻止关闭是false
            })
        }
    }

    //关闭弹窗
    closeModal() {
        this.props.onClose && this.props.onClose();
    }

    //初始化数据
    getModalData(){
        this.setState({
            pk_org: this.props.pk_org
        })
        ajax ({
            url: '/nccloud/hrhi/seqcontrol/GoSetupSeqcontrolAction.do',
            data: {
                'pk_org': this.state.pk_org,
            },
            success: res => {
                if(res.success){
                    let len = 0;
                    this.setState({
                        tableData: res.data
                    });
                    res.data.map((v,i)=>{
                        if(!v.isselected){
                            len++;
                        }
                    });
                    if(res.data.length == len){
                        this.setState({
                            checkedVal : "" //置空
                        })
                    }
                }
            }
        })
    }

    //选择设置类型
    changeChecked (v) {
        let changeVal = v.target.id;
        this.setState({
            checkedVal : changeVal
        })
    }

    renderTable(arr){
        let _this = this;
        const columns = [
            { title: _this.state.json['hihr-000026'], dataIndex: 'pk_seqcontrol_set', key: 'a', width: 80, render(pk_seqcontrol_set,data) {
                    // if(data.pk_seqcontrol_set && data.isselected){
                    //     _this.setState({
                    //         checkedVal: data.pk_seqcontrol_set
                    //     })
                    // }
                    return (
                        <input
                            className={'set-modal-radio'}
                            type="radio" 
                            name="radios" 
                            onClick={_this.changeChecked} 
                            id={pk_seqcontrol_set} 
                            defaultChecked={!!data.isselected}
                        />
                    );
                },
            },
            { title: _this.state.json['hihr-000027'],dataIndex: 'code',key: 'b', width: 80},
            { title: _this.state.json['hihr-000028'],dataIndex: 'name',key: 'c'},
            { title: _this.state.json['hihr-000029'],dataIndex: 'memo',key: 'd'},
        ];

        return (
            <NCTable
                columns={columns}
                data={arr}
            />
        )
    }

    render() {
        const {setModalVisible } = this.props;
        const {tableData} = this.state;
        return (
            <div class="nc-box">
                <NCModal
                    show={setModalVisible}
                    onHide={this.closeModal}
                    backdrop={'static'}
                >
                    <NCModalHeader closeButton={true}>
                        {this.state.json['hihr-000025']}
                    </NCModalHeader>
                    <NCModalBody>
                        {
                            tableData.length > 0 ? this.renderTable(tableData) : ''
                        }
                    </NCModalBody>
                    <NCModalFooter>
                        <NCButton colors="primary" onClick={()=>this.showPromptBox()}>
                           {this.state.json['hihr-000006']}
                        </NCButton>
                        <NCButton onClick={this.closeModal}>
                           {this.state.json['hihr-000007']}
                        </NCButton>
                    </NCModalFooter>
                </NCModal>
            </div>
        );
    }
}

export default ReptObjModal;
